from utils.baseResource import BaseResource
from utils.Response import response
from flask_restful import request, reqparse
from utils.db_seesion import execute, fetch_to_dict_pagetion, get_count
import time


class ProjectList(BaseResource):
    def get(self):
        pname = request.args.get('pname')
        page = request.args.get('page')
        size = request.args.get('size')
        if pname:
            seach_sql = 'select a.id,a.project_name,a.project_version,a.project_desc,a.create_time,a.update_time,b.nickname as create_user from project as a INNER JOIN users as b on a.create_user=b.id where project_name like :pname'
            result = fetch_to_dict_pagetion(seach_sql, params={'pname': '%{}%'.format(pname)}, page=page, page_size=size)
        else:
            seach_sql = 'select a.id,a.project_name,a.project_version,a.project_desc,a.create_time,a.update_time,b.nickname as create_user from project as a INNER JOIN users as b on a.create_user=b.id'
            result = fetch_to_dict_pagetion(seach_sql, page=page, page_size=size)
            print(result)

        return response(data=result)


class ProjectAdd(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pname', required=True, help='pname is required')
    paser.add_argument('pversion', required=True, help='pversion is required')
    paser.add_argument('pdesc', required=True, help='pdesc is required')

    def post(self):
        ProjectAdd.paser.parse_args()
        pname = self._datas.get("pname")
        serch_sql = "select count(*) as count from project where  project_name=:pname"
        count = get_count(sql=serch_sql, params={"pname": pname})
        if count > 0:
            return response(code=1001, msg="项目已存在")
        pversion = self._datas.get("pversion")
        pdesc = self._datas.get("pdesc")
        user_id = self._user_id
        create_time = time.time()
        update_time = time.time()

        insert_sql = 'insert into project (project_name,project_version,project_desc,create_time,create_user,update_time) values (:pname,:pversion,:pdesc,:create_time,:user_id,:update_time)'
        execute(sql=insert_sql,
                params={"pname": pname, "pversion": pversion, "pdesc": pdesc, "create_time": create_time,
                        "user_id": user_id, "update_time": update_time})
        return response()


class ProjectUpdate(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pid', required=True, help='pid is required')
    paser.add_argument('pname', required=True, help='pname is required')
    paser.add_argument('pversion', required=True, help='pversion is required')
    paser.add_argument('pdesc', required=True, help='pdesc is required')

    def post(self):
        ProjectUpdate.paser.parse_args()
        pid = self._datas.get('pid')
        checkPid_sql = 'select count(*) as count from project where id=:pid and is_delete=0'
        pidNum = get_count(sql=checkPid_sql, params={'pid': pid})
        if (pidNum != 1):
            return response(code=1001, msg='传入的项目id不合法')
        pname = self._datas.get('pname')
        serch_sql = "select count(*) as count from project where  project_name=:pname and id!=:pid"
        count = get_count(sql=serch_sql, params={"pname": pname, "pid": pid})
        if count > 0:
            return response(code=1001, msg="项目已存在")
        pversion = self._datas.get('pversion')
        pdesc = self._datas.get('pdesc')
        user_id = self._user_id
        update_time = time.time()
        update_sql = 'update project set project_name=:pname,project_version=:pversion,project_desc=:pdesc,update_time=:update_time where id=:pid'
        execute(sql=update_sql,
                params={'pname': pname, 'pversion': pversion, 'pdesc': pdesc, 'update_time': update_time, 'pid': pid})
        return response()

class ProjectDelete(BaseResource):
    paser = reqparse.RequestParser()
    paser.add_argument('pid',required=True,help='pid is required')

    def get(self):
        ProjectDelete.paser.parse_args()
        pid = request.agrs.get('pid')
        checkPid_sql = 'select count(*) as count from project where id=:pid and is_delete=0'
        pidNum = get_count(sql=checkPid_sql, params={'pid': pid})
        if (pidNum != 1):
            return response(code=1001, msg='传入的项目id不合法或以删除')
        delete_sql = 'update project as p inner join casesuite as c on p.id=c.project_id set p.is_delete=1 and c.is_delete=1 where p.id=:pid'
        execute(sql=delete_sql,params={'pid':pid})
        return response()



